package bean;

import dao.AcessoDAO;
import dao.FormarTime;
import dto.HoraBancoDto;
import dto.HoraDto;
import java.io.Serializable;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public final class ContadorBean implements Serializable {

    private long rede;
    private Time rede_tela;
    private long regressiva;
    private Time horainicial;
    private Time horafinal;
    private Time comercial;
    private long producao;
    private HoraDto hora;
    private AcessoDAO dao;

    public ContadorBean() {
        horainicial = new Time(0);
        horafinal = new Time(0);
        comercial = new Time(0);
        rede_tela = new Time(0);
        rede = System.currentTimeMillis();
        dao = new AcessoDAO();
        this.attDados();
    }

    public String getProducao() {
        String ret;

        if (producao > 0) {
            ret = FormarTime.formatarTimestamp(producao);

        } else {
            ret = "00:00:00";
        }

        return ret;
    }

    public void incrementaProducao() {

        long val1 = horafinal.getTime() - horainicial.getTime();

        String hr_comercial = FormarTime.formatarTimestamp(val1);
        SimpleDateFormat form = new SimpleDateFormat("HH:mm:ss");
        try {
            String com = comercial.toString();
            Date dt = form.parse(hr_comercial);
            Date comerc = form.parse(com);

            long result = dt.getTime() - comerc.getTime();

            producao = result;

            System.out.println(FormarTime.formatarTimestamp(result));

        } catch (Exception e) {
        }
    }

    public final void attDados() {
        hora = dao.ultimoRegistroPorPK();
    }

    public HoraDto getHora() {
        return hora;
    }

    public void setHora(HoraDto hora) {
        this.hora = hora;
    }

    public Time getComercial() {
        comercial.setTime(hora.getComercial());
        return comercial;
    }

    public void setComercial(Time comercial) {
        this.comercial = comercial;
    }

    public Time getHorafinal() {
        horafinal.setTime(hora.getHorafim());
        return horafinal;
    }

    public void setHorafinal(Time hora_final) {
        this.horafinal = hora_final;
    }

    public Time getHorainicial() {
        horainicial.setTime(hora.getHorainicial());
        return horainicial;
    }

    public void setHorainicial(Time hora_inicial) {
        this.horainicial = hora_inicial;
    }

    public Time getRede_tela() {
        rede_tela.setTime(rede);
        return rede_tela;
    }

    public void setRede_tela(Time rede_tela) {
        this.rede_tela = rede_tela;
    }

    public void incrementaRede() {
        rede = System.currentTimeMillis();
    }

    public String getRegressiva() {
        String ret = "00:00:00";

        if (regressiva > 0) {
            ret = FormarTime.formatarTimestamp(regressiva);
        }
        return ret;
    }

    public void incrementaRegressiva() {

        Time rd = new Time(rede);

        regressiva = horainicial.getTime() - rd.getTime();
    }

}
